
#include "StartGameScreen.h"


using namespace std;
//using namespace cgGame;


CStartGameScreen::CStartGameScreen(
							   HWND hwnd, 
							   LPDIRECT3DDEVICE9 d3dDevice)
							   :m_hWnd(hwnd), 
							   m_pD3DDevice(d3dDevice),
							   m_leftArrowKeyUp(true),
							   m_rightArrowKeyUp(true),
							   m_lButtonUp(true),
							   m_lButtonDown(false),
							   m_pBgSprite(NULL),
							   m_pGameState(NULL),
							   m_pStartSprite(NULL),
							   m_pStartOverSprite(NULL),
							   m_pStartDownSprite(NULL),
							   m_pFontRender(NULL),
							   m_pNextLevelSprite(NULL),
							   m_pPrevlevelSprite(NULL)
{
	m_timeStartGameOffset = 1.0f;
	m_startGame = false;

	GetCursorPos(&m_currMousrPos);
};


CStartGameScreen::~CStartGameScreen()
{
	SafeDelete(m_pFontRender);

	SafeDelete(m_pNextLevelSprite);
	SafeDelete(m_pPrevlevelSprite);

	SafeDelete(m_pStartDownSprite);
	SafeDelete(m_pStartOverSprite);
	SafeDelete(m_pStartSprite);

	SafeDelete(m_pBgSprite);
}



bool CStartGameScreen::InitScreenObjects()
{
	m_pGameState = CGameState::GetGameStateInstance();

	m_pBgSprite = new CD3DXSprite(m_pD3DDevice);
	m_pBgSprite->SetTextureScale("res\\menu\\start.png", g_screenWidth, g_screenHeight);
	m_pBgSprite->SetPosition(cgMath::cgVector2D(g_screenWidth / 2, g_screenHeight / 2));

	m_pStartSprite = new CD3DXSprite(m_pD3DDevice);
	m_pStartSprite->SetTextureScale("res\\menu\\startbtn.png", 200, 100);
	m_pStartSprite->SetPosition(cgMath::cgVector2D(400.0f, 450.0f));

	m_pStartOverSprite = new CD3DXSprite(m_pD3DDevice);
	m_pStartOverSprite->SetTextureScale("res\\menu\\startover.png", 200, 100);
	m_pStartOverSprite->SetPosition(cgMath::cgVector2D(400.0f, 450.0f));

	m_pStartDownSprite = new CD3DXSprite(m_pD3DDevice);
	m_pStartDownSprite->SetTextureScale("res\\menu\\startdown.png", 200, 100);
	m_pStartDownSprite->SetPosition(cgMath::cgVector2D(400.0f, 450.0f));

	m_pPrevlevelSprite = new CD3DXSprite(m_pD3DDevice);
	m_pPrevlevelSprite->SetTextureScale("res\\menu\\prevLevel.png", 80, 80);
	//m_pPrevlevelSprite->SetTextureScale("res\\menu\\prevLevel.png", 200, 100);
	m_pPrevlevelSprite->SetPosition(cgMath::cgVector2D(200.0f, 350.0f));

	m_pNextLevelSprite = new CD3DXSprite(m_pD3DDevice);
	m_pNextLevelSprite->SetTextureScale("res\\menu\\nextLevel.png", 80, 80);
	//m_pNextLevelSprite->SetTextureScale("res\\menu\\nextLevel.png", 200, 100);
	m_pNextLevelSprite->SetPosition(cgMath::cgVector2D(600.0f, 350.0f));

	m_pFontRender = new CFontRenderer(m_pD3DDevice, 24);

	////debug
	//m_pDebug = new CDebug();

	return true;
}



void CStartGameScreen::RenderObjects(float timeDelta)
{

	if (Game_CurrState_Prepareing == m_pGameState->GetGamecurrState())
	{
		m_pBgSprite->Render(timeDelta);

		m_pFontRender->Render(D3DXVECTOR3(g_screenWidth / 2, g_screenHeight / 2, 0),
			"Game Level : " + cgMath::FloatToString((float)m_pGameState->GetGameLevel()),
			true, 
			D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f),
			true,
			D3DXVECTOR2(g_screenWidth / 2 - 100.0f, g_screenHeight / 2));

		if (KEYDOWN(VK_LEFT) && m_leftArrowKeyUp )
		{
			m_pGameState->GameLevelDown();
			//m_pGameState->SetHunterFireSound(true);
			m_pGameState->SetMenuClickSound(true);
			m_leftArrowKeyUp = false;
		}
		if (KEYDOWN(VK_RIGHT) && m_rightArrowKeyUp)
		{
			m_pGameState->GameLevelUp();
			//m_pGameState->SetHunterFireSound(true);
			m_pGameState->SetMenuClickSound(true);
			m_rightArrowKeyUp = false;
		}

		GetCursorPos(&m_currMousrPos);
		ScreenToClient(m_hWnd, &m_currMousrPos);

		if (m_currMousrPos.x < m_pStartSprite->GetLeftTopPos().x ||
			m_currMousrPos.y < m_pStartSprite->GetLeftTopPos().y ||
			m_currMousrPos.x > m_pStartSprite->GetRightDownPos().x || 
			m_currMousrPos.y > m_pStartSprite->GetRightDownPos().y)
		{
			m_pStartSprite->Render(timeDelta);
		}
		else
		{
			m_pStartOverSprite->Render(timeDelta);
			if (KEYDOWN(VK_LBUTTON) && m_lButtonUp )
			{
				m_lButtonUp = false;
				m_pStartDownSprite->Render(timeDelta);
				m_pGameState->SetMenuClickSound(true);
				//m_pGameState->HunterLifesUp();
				//m_pGameState->InitLevelDatas();
				//list<CCharacter*>::iterator iter = m_pGameState->GetCharacterListBegin();
				//(*iter)->SetTypeAndContr(Character_Type_Hunter);
				//m_pGameState->InitGameState(0, 90);
			}
			if (KEYUP(VK_LBUTTON) && m_lButtonDown)
			{
				m_pStartDownSprite->Render(timeDelta);
				m_lButtonDown = false;
				m_startGame = true;

			}
		}

		m_pPrevlevelSprite->Render(timeDelta);
		if (m_currMousrPos.x > m_pPrevlevelSprite->GetLeftTopPos().x &&
			m_currMousrPos.y > m_pPrevlevelSprite->GetLeftTopPos().y &&
			m_currMousrPos.x < m_pPrevlevelSprite->GetRightDownPos().x && 
			m_currMousrPos.y < m_pPrevlevelSprite->GetRightDownPos().y)
		{
			if (KEYDOWN(VK_LBUTTON) && m_lButtonUp)
			{
				m_lButtonUp = false;
				//MessageBox(m_hWnd, "hit", "hit", 0);
				if (false == m_startGame)
				{
					m_pGameState->GameLevelDown();
					m_pGameState->SetHunterFireSound(true);
				}
			}
		}

		m_pNextLevelSprite->Render(timeDelta);
		if (m_currMousrPos.x > m_pNextLevelSprite->GetLeftTopPos().x &&
			m_currMousrPos.y > m_pNextLevelSprite->GetLeftTopPos().y &&
			m_currMousrPos.x < m_pNextLevelSprite->GetRightDownPos().x &&
			m_currMousrPos.y < m_pNextLevelSprite->GetRightDownPos().y)
		{
			if (KEYDOWN(VK_LBUTTON) && m_lButtonUp)
			{
				m_lButtonUp = false;
				//MessageBox(m_hWnd, "hit", "hit", 0);
				if (false == m_startGame)
				{
					m_pGameState->GameLevelUp();
					m_pGameState->SetHunterFireSound(true);
				}
			}
		}


		if (true == m_startGame)
		{
			m_timeStartGameOffset -= timeDelta;
			if (m_timeStartGameOffset <= 0)
			{
				m_timeStartGameOffset = 5.0f;
				m_startGame = false;
				m_pGameState->HunterLifesUp();
				m_pGameState->InitLevelDatas();
				list<CCharacter*>::iterator iter = m_pGameState->GetCharacterListBegin();
				(*iter)->SetTypeAndContr(Character_Type_Hunter);
			}
		}


		if (KEYUP(VK_LEFT))
		{
			m_leftArrowKeyUp = true;
		}
		else
		{
			m_leftArrowKeyUp = false;
		}
		if (KEYUP(VK_RIGHT))
		{
			m_rightArrowKeyUp = true;
		}
		else
		{
			m_rightArrowKeyUp = false;
		}
		if (KEYUP(VK_LBUTTON))
		{
			m_lButtonUp = true;
		}
		else
		{
			m_lButtonUp = false;
		}
		if (KEYDOWN(VK_LBUTTON))
		{
			m_lButtonDown = true;
		}
		else
		{
			m_lButtonDown = false;
		}
	}

}